import java.util.HashMap;
import java.util.Map;

import au.com.bytecode.opencsv.CSVReader;

public class QandAList {

	public String[] getHeader() throws Exception {
		if (this.header == null)
			throw new Exception("this.header == null");
		return this.header;
	}

	public Map<Integer, Question> getQuestionMap() throws Exception {
		if (this.questionMap == null)
			throw new Exception("this.questionMap == null");
		return this.questionMap;
	}

	public Map<Integer, Map<Character, Answer>> getAnswerMap() throws Exception {
		if (this.answerMap == null)
			throw new Exception("this.answerMap == null");
		return this.answerMap;
	}

	public Map<Integer, Map<Character, AnswerKey>> getAnswerKeyMap()
			throws Exception {
		if (this.answerKeyMap == null)
			throw new Exception("this.answerKeyMap == null");
		return this.answerKeyMap;
	}

	private String[] header;
	private Map<Integer, Question> questionMap;
	private Map<Integer, Map<Character, Answer>> answerMap;
	private Map<Integer, Map<Character, AnswerKey>> answerKeyMap;

	public void putAllQAndA(CSVReader r) throws Exception {
		if (this.header != null)
			throw new Exception("this.header != null");

		if (this.questionMap != null)
			throw new Exception("this.questionMap != null");
		this.questionMap = new HashMap<Integer, Question>();

		if (this.answerMap != null)
			throw new Exception("this.answerMap != null");
		this.answerMap = new HashMap<Integer, Map<Character, Answer>>();

		String[] nextLine = null;
		while ((nextLine = r.readNext()) != null) {
			if (this.header == null)
				this.header = nextLine;
			else {
				Integer ididx = 0;
				Integer qidx = 1;

				Integer qid = Integer.parseInt(nextLine[ididx]);
				Question question = new Question(qid, nextLine[qidx]);

				this.questionMap.put(qid, question);

				Map<Character, Answer> answers = new HashMap<Character, Answer>();

				for (Integer cidx = 0; cidx < nextLine.length; cidx++) {
					if (cidx.equals(ididx) || cidx.equals(qidx))
						continue;

					char aid = this.header[cidx].toCharArray()[0];
					String a = nextLine[cidx];
					if (!a.trim().isEmpty())
						answers.put(aid, new Answer( qid, aid, a));
				}
				this.answerMap.put(qid, answers);
			}
		}
	}

	public void putAKeys(CSVReader r) throws Exception {
		if (this.answerKeyMap != null)
			throw new Exception("this.answerKeyMap != null");
		this.answerKeyMap = new HashMap<Integer, Map<Character, AnswerKey>>();

		String[] keyHeader = null;
		String[] nextLine = null;
		while ((nextLine = r.readNext()) != null) {
			if (keyHeader == null) {
				keyHeader = nextLine;
			} else {
				Integer ididx = 0;//
				Integer qid = Integer.parseInt(nextLine[ididx]);

				Map<Character, AnswerKey> answersKey = new HashMap<Character, AnswerKey>();

				for (Integer cidx = 0; cidx < nextLine.length; cidx++) {
					if (cidx.equals(ididx))
						continue;

					char aid = keyHeader[cidx].toCharArray()[0];
					Answer a = this.answerMap.get(qid).get(aid);
					Boolean ak = Boolean.parseBoolean(nextLine[cidx]);
					answersKey.put(aid, new AnswerKey(a, ak));
				}
				this.answerKeyMap.put(qid, answersKey);
			}
		}

	}
}
